const express = require('express')
const router = express.Router()
const path = require('path')
const article = require('../models/article')
const bodyPaser = require('../tools/bodyPaser')

router.use(express.static('./static'))

router.get('/', (req, res) => {
    res.sendFile(path.resolve('./static/write.html'))
})
router.get('/getAllArticle', (req, res) => {
    article.find({}).then((result) => {
        res.send(result)
    })
})
router.post('/deleteArticle', bodyPaser, (req, res) => {
    var { id } = req.body
    article.remove({ _id: id }).then(() => {
        res.send('删除成功')
    })
})
router.post('/addArticle', bodyPaser, (req, res) => {
    var { title, content, time, classify, count } = req.body
    var a = new article({
        title: title,
        content: content,
        time: time,
        classify: classify,
        count: count
    })
    a.save().then(() => {
        res.send('文章发布成功')
    })
})

module.exports = router